package com.example.demo.Controller;



import com.example.demo.Entity.Shelf;
import com.example.demo.Entity.Warehouse;
import com.example.demo.Service.ShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/shelves")
@CrossOrigin(origins = "*")//允许所有同源访问，防止出现跨域问题
public class ShelfController {

    private final ShelfService shelfService;

    @Autowired
    public ShelfController(ShelfService shelfService) {
        this.shelfService = shelfService;
    }

    @PostMapping
    public Shelf createShelf(@RequestBody Shelf shelf) {
        return shelfService.createShelf(shelf);
    }

    @GetMapping("/{id}")
    public Optional<Shelf> getShelfById(@PathVariable Long id) {
        return shelfService.getShelfById(id);
    }

    @GetMapping
    public List<Shelf> getAllShelves() {
        return shelfService.getAllShelves();
    }

    @PutMapping("/{id}")
    public Shelf updateShelf(@PathVariable Long id, @RequestBody Shelf shelf) {
        return shelfService.updateShelf(id, shelf);
    }

    @DeleteMapping("/{id}")
    public boolean deleteShelf(@PathVariable Long id) {
        return shelfService.deleteShelf(id);
    }

    @GetMapping("/warehouse/{warehouseId}")
    public List<Shelf> findByWarehouse(@PathVariable Long warehouseId) {
        Warehouse warehouse = new Warehouse();
        warehouse.setId(warehouseId);
        return shelfService.findByWarehouse(warehouse);
    }

    @GetMapping("/location/{location}")
    public List<Shelf> findByLocation(@PathVariable String location) {
        return shelfService.findByLocation(location);
    }
    

    @GetMapping("/max-weight/{maxWeight}")
    public List<Shelf> findByMaxWeight(@PathVariable Double maxWeight) {
        return shelfService.findByMaxWeight(maxWeight);
    }

    @GetMapping("/current-weight/{currentWeight}")
    public List<Shelf> findByCurrentWeight(@PathVariable Double currentWeight) {
        return shelfService.findByCurrentWeight(currentWeight);
    }
}

